﻿#include "BTEItemWidget.h"
#include <QMouseEvent>
#include <QApplication>
#include "DataTrans.h"
#include "BTEServer.h"
#include <QFontMetrics>

BTEItemWidget::BTEItemWidget(QWidget *parent)
	: QWidget(parent)
{
	m_mouseDown = false;
	m_checked = false;	
	m_status = DeviceInfo::Status_Complete;
	
	memset(&m_rtdata, 0, sizeof(m_rtdata));
	this->setContextMenuPolicy(Qt::CustomContextMenu);		

	m_labels << tr("状态") << tr("工步序号") << tr("循环序号") << tr("工步时间") << tr("电压(V)")
		<< tr("电流(A)") << tr("充电容量(Ah)") << tr("充电能量(Wh)");	
	
	m_scale = 1;		
	m_fontTitle = font();
	m_fontTitle.setPixelSize(14);    
    
    setSize(Item_Normal);    
    updateToolTip();
}

BTEItemWidget::~BTEItemWidget()
{
}

void BTEItemWidget::getDispInfo(QStringList &vars)
{
    QString status = tr("正常");
    if (m_rtdata.workStatus() == stru_BTS_CMD_SEND_RTDATA76::WORK_ERROR_STOP || m_rtdata.PrtCode != 0)    
        status = PackDefine::ErrorList[m_rtdata.PrtCode];    

    for (int i = 0; i < m_labels.size(); i++)
        vars << QString();

    vars[0] = status;
    vars[1] = QString::number(m_rtdata.StepID);
    vars[2] = QString::number(m_rtdata.CycleStatus);
    vars[3] = DataTrans::fromTimeUnitSec(m_rtdata.TestTime);
    vars[4] = QString::number(DataTrans::fromElectricUnit(m_rtdata.Voltage), 'f', 4);
    vars[5] = QString::number(DataTrans::fromVoltageUnit(m_rtdata.Current), 'f', 4);
    if (m_rtdata.StepType == STEP_TYPE_2)
    {
        m_labels[6] = tr("放电容量(Ah)");
        m_labels[7] = tr("放电能量(Wh)");
        vars[6] = QString::number(DataTrans::fromCapUnit(m_rtdata.DisChargeCap), 'f', 4);
        vars[7] = QString::number(DataTrans::fromEngUnit(m_rtdata.DisChargeEng), 'f', 4);
    }
    else
    {
        m_labels[6] = tr("充电容量(Ah)");
        m_labels[7] = tr("充电能量(Wh)");
        vars[6] = QString::number(DataTrans::fromCapUnit(m_rtdata.ChargeCap), 'f', 4);
        vars[7] = QString::number(DataTrans::fromEngUnit(m_rtdata.ChargeEng), 'f', 4);
    }
}

void BTEItemWidget::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	QPen black(Qt::black);

	QColor bg_color = QColor(192, 192, 192);
    if(!isEnabled())
        bg_color = QColor(170, 175, 186);

	QColor ch_color = QColor(128, 64, 0);
	QColor title_color = QColor(0, 0, 0);
	QColor status_color = QColor(0, 0, 0);

	QRect rc = rect();
	int w = width();
	int h = height();

	painter.fillRect(rc, bg_color);
	painter.setPen(QPen(QColor(129, 129, 129), 2));

	int border_gap = 6;
	int border_top = border_gap + 1, border_topw = 80, border_toph = 16;
	int border_top2 = border_top + border_toph;
	int border_top_x1 = (w - border_topw) / 2;
	int border_top_x2 = border_top_x1 + border_topw;
	int border_left = border_gap;
	int border_right = w - border_gap;
	int border_bottom = h - border_gap;

	auto cellColor = bgColor();
	if (cellColor != bg_color)
	{
		painter.fillRect(QRect(QPoint(border_top_x1, border_top), QPoint(border_top_x2, border_top2)), cellColor);
		painter.fillRect(QRect(QPoint(border_left, border_top2), QPoint(border_right, border_bottom)), cellColor);
	}

	//上
	painter.drawLine(QPoint(border_top_x1, border_top), QPoint(border_top_x2, border_top));
	painter.drawLine(QPoint(border_top_x1, border_top), QPoint(border_top_x1, border_top2));
	painter.drawLine(QPoint(border_left, border_top2), QPoint(border_top_x1, border_top2));
	painter.drawLine(QPoint(border_top_x2, border_top2), QPoint(border_right, border_top2));

	//左
	painter.drawLine(QPoint(border_left, border_top2), QPoint(border_left, border_bottom));

	//右下
	painter.setPen(QPen(Qt::white, 2));
	painter.drawLine(QPoint(border_top_x2, border_top), QPoint(border_top_x2, border_top2));
	painter.drawLine(QPoint(border_right, border_top2), QPoint(border_right, border_bottom));
	painter.drawLine(QPoint(border_left, border_bottom), QPoint(border_right, border_bottom));	

	if (m_checked)
	{
		QRect border(QPoint(border_left, border_top), QPoint(border_right, border_bottom));
		border.adjust(-2, -2, 1, 1);
		painter.setPen(QPen(Qt::yellow, 2));
		painter.drawRect(border);
	}

	QString title;
	getTitle(title, title_color);

	QString status = tr("正常");
	if (m_rtdata.workStatus() == stru_BTS_CMD_SEND_RTDATA76::WORK_ERROR_STOP || m_rtdata.PrtCode != 0)
	{
		status_color = QColor(200, 0, 0);
		status = PackDefine::ErrorList[m_rtdata.PrtCode];
	}
    if (cellColor == QColor(180, 40, 80)) //反色 
    {
        status_color = QColor(0, 0, 0);
		ch_color = QColor(0, 0, 0);
    }

	QStringList vars;
    getDispInfo(vars);	

	int title_y = border_top2 + 24;
	QRect ch_rc = QRect(QPoint(border_top_x1, border_top), QPoint(border_top_x2, border_top2));
	QRect title_rc = QRect(QPoint(border_left, border_top2), QPoint(border_right - 20, title_y));

	painter.setPen(ch_color);
	painter.setFont(m_fontTitle);
	painter.drawText(ch_rc, m_channel, QTextOption(Qt::AlignCenter));

	painter.setFont(m_font);
	painter.setPen(title_color);
	painter.drawText(title_rc, title, Qt::AlignBottom | Qt::AlignRight);

	painter.setPen(black);
	int text_x, text_y, text_gap,text_len;

	if (m_scale == 1)
	{
		text_x = border_left + 4;
		text_len = width() - text_x - 4;
	}
	else
	{
		text_x = width() * 1 / 4;
		text_len = width() - 2 * text_x;
	}	
    if (m_baseHeight == 250)
	    text_gap = text_len/2 + (text_len/2 - 60);
    else
        text_gap = text_len/2 + (text_len/2 - 45);
	text_y = title_y + 24;

	for (int i = 0; i < m_labels.size(); i++)
	{
		painter.drawText(text_x, text_y, m_labels[i]);
		if (i == 0)
		{
            QFontMetrics metrics(m_font);
            int text_width = metrics.width(vars[i]);

			painter.setPen(status_color);            			
            if (text_x + text_gap + text_width > width())
            {
                painter.drawText(w - text_width - 8, text_y, vars[i]);
            }
            else
                painter.drawText(text_x + text_gap, text_y, vars[i]);

			painter.setPen(black);
		}
		else
			painter.drawText(text_x + text_gap, text_y, vars[i]);

        if(m_baseHeight == 250)
		    text_y += 22;
        else
            text_y += 16;
	}	
}

void BTEItemWidget::mousePressEvent(QMouseEvent *event)
{    
	if(event->button() == Qt::LeftButton || event->button() == Qt::RightButton)
		m_mouseDown = true;

    QWidget::mousePressEvent(event);
}

void BTEItemWidget::mouseReleaseEvent(QMouseEvent *event)
{
	QPoint pos = event->pos();
	if (m_mouseDown && rect().contains(pos))
	{
		bool old_check = m_checked;		
		if (QApplication::keyboardModifiers() != Qt::ControlModifier)
		{
			if (!m_checked)
			{
				m_checked = true;
				update();
			}

			if (event->button() == Qt::LeftButton 
				|| (event->button() == Qt::RightButton && !old_check))
				emit toggled(true);
		}
		else
		{
			m_checked = !m_checked;
			update();
		}
	}	

	m_mouseDown = false;
    QWidget::mouseReleaseEvent(event);
}

void BTEItemWidget::setCheck(bool flag)
{	
	if (m_checked != flag)
	{
		m_checked = flag;
		update();
	}
}

bool BTEItemWidget::isChecked()
{
	return m_checked;
}

void BTEItemWidget::setSize(int itemSize)
{
    if (itemSize == Item_Normal)
    {
        m_font = font();
        m_font.setPixelSize(12);

        m_baseWidth = 170;
        m_baseHeight = 250;
    }
    else if (itemSize == Item_Small)
    {
        m_font = font();
        m_font.setPixelSize(9);

        m_baseWidth = 120;
        m_baseHeight = 200;
    }
    setScale(m_scale);
}

QColor BTEItemWidget::bgColor()
{
    if(!isEnabled())
        return QColor(170, 175, 186);

	if (m_status == DeviceInfo::Status_Complete)	
	{
		return QColor(192, 192, 192);
	}
	else if (m_status == DeviceInfo::Status_Stop)
	{
		if(m_rtdata.PrtCode != 0)
			return QColor(180, 40, 80);
		else 
			return QColor(240, 240, 240);
	}	
	else if (m_status == DeviceInfo::Status_OffLine && m_rtdata.PrtCode != 0)
	{
		return QColor(180, 40, 80);
	}
	else
	{
		if (m_rtdata.StepType == STEP_TYPE_1 
			|| m_rtdata.StepType == STEP_TYPE_3
			|| m_rtdata.StepType == STEP_TYPE_7)
			return QColor(15, 175, 0);
		else if (m_rtdata.StepType == STEP_TYPE_2)
			return QColor(180, 180, 20);	
		else if (m_rtdata.StepType == STEP_TYPE_STOP)
			return QColor(192, 192, 192);
		else
			return QColor(204, 255, 255);
	}
}

void BTEItemWidget::setItemIndex(int idx)
{
	m_itemIndex = idx;
}

int BTEItemWidget::itemIndex()
{
	return m_itemIndex;
}

void BTEItemWidget::setChannelText(QString ch)
{
	m_channel = ch;
}

int BTEItemWidget::status()
{
	return m_status;
}

void BTEItemWidget::setStatus(int status)
{
	m_status = status;	
	update();
}

void BTEItemWidget::setScale(int scale)
{
	int gap = 3;

	m_scale = scale;	
	setFixedSize(m_baseWidth* scale + (scale - 1) * gap, m_baseHeight);
}

int BTEItemWidget::scale()
{
	return m_scale;
}

void BTEItemWidget::clear()
{
	memset(&m_rtdata, 0, sizeof(m_rtdata));	
	update();
}

stru_BTS_CMD_SEND_RTDATA76 BTEItemWidget::data()
{
	return m_rtdata;
}

void BTEItemWidget::setData(const stru_BTS_CMD_SEND_RTDATA76 &pack)
{
	m_rtdata = pack;
    updateToolTip();
	update();
}

void BTEItemWidget::updateToolTip()
{
    QStringList vars;
    getDispInfo(vars);

    QString tips;
    for (int i = 0; i < m_labels.size(); i++)
    {
        tips += m_labels[i] + ": " + vars[i] + "\n";
    }
    setToolTip(tips);
}

void BTEItemWidget::getTitle(QString &title, QColor &color)
{
	color = QColor(0, 0, 0);

	int stepType = m_rtdata.StepType;
	if (m_status == DeviceInfo::Status_Complete)
		title = tr("完成");
	else if (m_status == DeviceInfo::Status_WaitStart)
		title = tr("正在启动");
	else if (m_status == DeviceInfo::Status_Run)
	{
		if(stepType == STEP_TYPE_NULL)
			title = tr("正在启动");
		else
			title = PackDefine::StepNameList[stepType];
	}
	else if (m_status == DeviceInfo::Status_WaitStop)
	{
		title = tr("正在停止");
	}
	else if (m_status == DeviceInfo::Status_Stop)
	{
		title = tr("暂停");
	}
	else if (m_status == DeviceInfo::Status_WaitJump)
	{
		title = tr("正在跳转");
	}
	else if (m_status == DeviceInfo::Status_Error)
	{
		title = tr("内部错误");
	}
	else if (m_status == DeviceInfo::Status_OffLine)
	{
		title = tr("离线");
	}
	else
	{
		Q_ASSERT(0);
	}
}
